/*
 * $Id$
 *
 * Copyright ( c ) 2014 XuNuo Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of XuNuo
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with XuNuo Corporation or a XuNuo
 * authorized reseller (the "License Agreement"). XuNuo may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, XuNuo DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. XuNuo DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * XuNuo SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF XuNuo HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. XuNuo' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE XuNuo SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.phamapply.client.widget.checkdispense;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.th.supcom.pds.common.client.commom.SimpleMethod;
import com.th.supcom.pds.common.client.widget.PdsGroupGrid;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.phamapply.client.model.InpPhamDispenseDetailModel;

/**
 * 住院摆药明细Grid
 * 
 * @author 杨冲
 */
public class InpCheckDispenseDetailGrid extends PdsGroupGrid <InpPhamDispenseDetailModel>
{
     
   private  ColumnModel m_columnModel;
    @Override
    public ColumnModel createColumnModel ()
    {
        List <ColumnConfig> configs = new ArrayList <ColumnConfig> ();
        ColumnConfig column = null;
        column = new ColumnConfig ("phamName", "药品名称", 200);
        configs.add (column);
        column = new ColumnConfig ("phamSpec", "药品规格", 200);
        configs.add (column);
        column = new ColumnConfig ("dispenseAmount", "摆药数量", 100);
        column.setRenderer (getApplyAmountRenderer ());
        configs.add (column);
        column = new ColumnConfig ("costs", "摆药金额", 100);
        column.setRenderer (getCostsRenderer ());
        column.setAlignment (HorizontalAlignment.RIGHT);
        configs.add (column);
        m_columnModel=new ColumnModel (configs);
        return m_columnModel;
    }

    /**
     * 获取申请数量Renderer
     * 
     * @return
     */
    private GridCellRenderer <InpPhamDispenseDetailModel> getApplyAmountRenderer ()
    {
        return new GridCellRenderer <InpPhamDispenseDetailModel> ()
        {

            @Override
            public Object render (InpPhamDispenseDetailModel model, String property, ColumnData config, int rowIndex, int colIndex,
                                  ListStore <InpPhamDispenseDetailModel> store, Grid <InpPhamDispenseDetailModel> grid)
            {
                return model.getDispenseAmount () + model.getDispenseUnitName ();
            }
        };
    }

    /**
     * 获取摆药金额Renderer
     * 
     * @return
     */
    private GridCellRenderer <InpPhamDispenseDetailModel> getCostsRenderer ()
    {
        return new GridCellRenderer <InpPhamDispenseDetailModel> ()
        {

            @Override
            public Object render (InpPhamDispenseDetailModel model, String property, ColumnData config, int rowIndex, int colIndex,
                                  ListStore <InpPhamDispenseDetailModel> store, Grid <InpPhamDispenseDetailModel> grid)
            {
                // 保留两位小数
                Double cost = SimpleMethod.retainDoubleTwo (model.getCosts ());
                return ClientUtils.formatNumTwo (cost);
            }
        };
    }

    /**
     * 构造函数
     */
    public InpCheckDispenseDetailGrid ()
    {
        getGroupingStore ().groupBy ("paiVisitId");

        GroupingView view = new GroupingView ();
        view.setEmptyText ("暂时没有您需要的数据...");
        view.setShowGroupedColumn (false);
        view.setGroupRenderer (new GridGroupRenderer ()
        {
            public String render (GroupColumnData data)
            {
                Double totalCost = 0D;
                for (ModelData model : data.models)
                {
                    InpPhamDispenseDetailModel detailModel = (InpPhamDispenseDetailModel) model;
                    totalCost += detailModel.getCosts ();
                }
                totalCost = SimpleMethod.retainDoubleTwo (totalCost);
                return getGroupTitle ((InpPhamDispenseDetailModel) data.models.get (0), data.models.size (), totalCost);
            }
        });
        setGridView (view);
    }

    /**
     * 获取分组标题字符串
     * 
     * @return
     */
    private String getGroupTitle (InpPhamDispenseDetailModel model, int size, Double totalCost)
    {
        if(model.getBalance ()==null){
            model.setBalance (0.0);
        }
       /* String moneyStr =totalCost.doubleValue ()>model.getBalance ().doubleValue ()?SimpleMethod.moneyStyle (SimpleMethod.retainDoubleTwo (model.getBalance ())):SimpleMethod.retainDoubleTwo (model.getBalance ())+"";
        String totalCostStr = totalCost.doubleValue ()>model.getBalance ().doubleValue ()?SimpleMethod.moneyStyle (totalCost):totalCost+"";*/
        return "床号：" + model.getBedNo () + " 住院号：" + model.getPaiVisitId ()+ " 姓名：" + model.getPatName () + " 性别：" + model.getGenderName () + " 费别：" + model.getChargeTypeName ()
                + " 合计：" + totalCost.doubleValue () + " (" + size + "条明细)";
    }

    /**
     * 获取Store
     * 
     * @return
     */
    public GroupingStore <InpPhamDispenseDetailModel> getStore ()
    {
        return getGroupingStore ();
    }

    /**
     * 获取Grid
     * 
     * @return
     */
    public Grid <InpPhamDispenseDetailModel> getGroupGrid ()
    {
        return getGrid ();
    }

    public ColumnModel getColumnModel ()
    {
    
        return m_columnModel;
    }

}
